package com.kyyc.luff.gateway.exception;


import com.kyyc.luff.core.exception.MyRuntimeException;
import com.kyyc.luff.core.result.ResultEnum;
import com.kyyc.luff.core.result.ResultView;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;


/**
 * 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @Value("${spring.servlet.multipart.max-request-size}")
    private String maxRequestSize;

    @ExceptionHandler(value = ZuulException.class)
    public ResultView zuulExceptionHandler(MyRuntimeException e) {
        log.error("网关处理异常 => {}", e);
        return ResultView.error(e.getMessage());
    }

    @ExceptionHandler(value = MyRuntimeException.class)
    public ResultView defaultErrorHandler(MyRuntimeException e) {
        log.error("自定义运行时异常 => {}", e);
        return ResultView.error(e.getMessage());
    }

    @ExceptionHandler(value = RuntimeException.class)
    public ResultView defaultErrorHandler(RuntimeException e) {

        if (e instanceof MaxUploadSizeExceededException) {
            return ResultView.error("上传的文件不能大于" + maxRequestSize + "！");
        }
        log.error("系统运行时异常 => {}", e);
        return ResultView.error(ResultEnum.CODE_666);
    }
}
